@extends('layouts.app')
@include('good.sidebar')
@section('content')
    @php
        use Illuminate\Support\Facades\Auth;
        use App\Modules\User\Models\Admin;
        $user = Auth::user();
    @endphp
    <div class="content">
        <div class="row custom-invoice">
            <div class="row">
                <form class="form form-inline" method="get" action="{{ route('admin.good.index') }}">
                    <input name="shop_id" value="{{ $shopId }}" type="hidden"/>
                    <div class="form-group"  >
                        <input style="width:200px;margin-left: 15px;margin-bottom: 10px" class="form-control" type="text" name="id" value="{{ $id }}" placeholder="商品ID">
                    </div>
                    <div class="form-group"  >
                        <select style="width:200px;margin-left: 10px;margin-bottom: 10px" id="top_category_id" name="top_category_id" class="form-control">
                            <option value="0">顶级分类</option>
                            @foreach($topCategory as $key => $value)
                                <option value="{{ $key }}" {{ $top_category_id == $key ? "selected":""}}>{{ $value }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group"  >
                        <select style="width:200px;margin-left: 10px;margin-bottom: 10px" id="first_category_id" name="first_category_id" class="form-control">
                            <option value="">一级分类</option>
                            @if (!empty($first_category_id))
                            @foreach($firstCategory as $key => $value)
                                <option value="{{ $key }}" {{ $first_category_id == $key ? "selected":""}}>{{ $value }}</option>
                            @endforeach
                            @endif
                        </select>
                    </div>
                    <div class="form-group"  >
                        <select style="width:200px;margin-left: 10px;margin-bottom: 10px" id="category_id" name="category_id" class="form-control">
                            <option value="">二级分类</option>
                            @if (!empty($category_id))
                            @foreach($category as $key => $value)
                                <option value="{{ $key }}" {{ $category_id == $key ? "selected":""}}>{{ $value }}</option>
                            @endforeach
                            @endif
                        </select>
                    </div>
                    <div class="form-group"  >
                        <select style="width:200px;margin-left: 10px;margin-bottom: 10px" id="brand_id" name="brand_id" class="form-control">
                            <option value="">全部品牌</option>
                            @if (!empty($brand_id))
                            @foreach($brand as $key => $value)
                                <option value="{{ $key }}" {{ $brand_id == $key ? "selected":""}}>{{ $value }}</option>
                            @endforeach
                            @endif
                        </select>
                    </div>
                    <div class="form-group"  >
                        <input style="width:200px;margin-left: 10px;margin-bottom: 10px" class="form-control" type="text" name="name" value="{{ $name }}" placeholder="商品名">
                    </div>
                    <div class="form-group" style="width:100px;margin-left: 15px;margin-bottom: 10px">
                        <button class="btn btn-success btn-ms" type="submit">搜索</button>
                    </div>
                </form>
            </div>
            @if($user->is_merchant_staff == Admin::MERCHANT_AUTH)
                @can('merchant_staff_and_package_add')
                    <a class="btn btn-success" href="{{ route("admin.good.create", ['shop_id' => $shopId]) }}">添加商品</a>
                @endcan
            @else
                @can('good-add')
                    <a class="btn btn-success" href="{{ route("admin.good.create", ['shop_id' => $shopId]) }}">添加商品</a>
                @endcan
            @endif
            <br/>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th style="min-width:50px">商品ID</th>
                    <th style="min-width:250px">商品名</th>
                    <th style="min-width:150px">所属商铺</th>
                    <th style="min-width:80px">品牌</th>
                    <th style="min-width:80px">一级分类</th>
                    <th style="min-width:80px">二级分类</th>
                    <th style="min-width:80px">划线价</th>
                    <th style="min-width:80px">价格</th>
                    <th style="min-width:80px">状态</th>
                    <th style="min-width:100px">是否免费购</th>
                    <th style="min-width:250px">操作</th>
                    <th style="min-width:200px">排序权重</th>
                </tr>
                </thead>
                <tbody>
                @foreach($data as $item)
                    <tr>
                        <td>{{ $item->id }}</td>
                        <td>{{ $item->name }}</td>
                        <td>{{ $shop->get($item->shop_id)->name }}</td>
                        <td>{{ $item->brand }}</td>
                        <td>{{ $item->first_category_name }}</td>
                        <td>{{ $item->second_category_name }}</td>
                        <td>{{ $item->display_price/100 }} </td>
                        <td>{{ $item->price/100 }}</td>
                        <td>{{ \App\Modules\Good\Models\Good::$showStatus[$item->status] }}</td>
                        <td>{!! $item->is_free ? "<i class='fa fa-check'></i>":""  !!}</td>
                        <td>
                            @if($user->is_merchant_staff == Admin::MERCHANT_AUTH)
{{--                                @can('merchant_staff_and_package_rule')--}}
{{--                                    <a class="btn btn-info btn-xs"--}}
{{--                                       href="{{ route('admin.good.rules.edit', ['id' => $item->id]) }}"><i--}}
{{--                                            class="fa fa-exchange"></i> 返利规则</a>--}}
{{--                                @endcan--}}

                                @can('merchant_staff_and_package_edit')
                                    <a class="btn btn-primary btn-xs"
                                       href="{{ route('admin.good.edit', ['id' => $item->id, 'shop_id' => $shopId, 'redirect' => url()->full()]) }}"><i
                                            class="fa fa-edit"></i> 编辑</a>
                                @endcan
                                @can('merchant_staff_and_package_comment')
                                    <a class="btn btn-success btn-xs"
                                       href="{{ route('admin.good.comment', ['id' => $item->id, 'shop_id' => $shopId]) }}"><i
                                            class="fa fa-comment-o"></i> 评论</a>
                                @endcan
                                @if($item->status == \App\Modules\Good\Models\Good::STATUE_OFF)
                                    @can('merchant_staff_up')
                                        <a class="btn btn-primary btn-xs"
                                           href="{{ route('admin.good.status', ['id' => $item->id]) }}"><i
                                                class="fa fa-circle-o-notch"></i> 上架</a>
                                    @endcan
                                @else
                                    @can('merchant_staff_down')
                                        <a class="btn btn-danger btn-xs"
                                           href="{{ route('admin.good.status', ['id' => $item->id]) }}"><i
                                                class="fa fa-ban"></i> 下架</a>
                                    @endcan
                                @endif
                            @else
{{--                                @can('good-rule')--}}
{{--                                    <a class="btn btn-info btn-xs"--}}
{{--                                       href="{{ route('admin.good.rules.edit', ['id' => $item->id]) }}"><i--}}
{{--                                            class="fa fa-exchange"></i> 返利规则</a>--}}
{{--                                @endcan--}}
                                @can('good-edit')
                                    <a class="btn btn-primary btn-xs"
                                       href="{{ route('admin.good.edit', ['id' => $item->id, 'shop_id' => $shopId, 'redirect' => url()->full()]) }}"><i
                                            class="fa fa-edit"></i> 编辑</a>
                                @endcan
                                @can('good-comment')
                                    <a class="btn btn-success btn-xs"
                                       href="{{ route('admin.good.comment', ['id' => $item->id, 'shop_id' => $shopId]) }}"><i
                                            class="fa fa-comment-o"></i> 评论</a>
                                @endcan
                                @if($item->status == \App\Modules\Good\Models\Good::STATUE_OFF)
                                    @can('good_up')
                                        <a class="btn btn-primary btn-xs"
                                           href="{{ route('admin.good.status', ['id' => $item->id]) }}"><i
                                                class="fa fa-circle-o-notch"></i> 上架</a>
                                    @endcan
                                @else
                                    @can('good_down')
                                        <a class="btn btn-danger btn-xs"
                                           href="{{ route('admin.good.status', ['id' => $item->id]) }}"><i
                                                class="fa fa-ban"></i> 下架</a>
                                    @endcan
                                @endif

                            @endif

                        </td>
                        <td>
                            <input type="text" class="col-xs-5 change-sort" data-id="{{ $item->id }}"
                                   value="{{ $item->sort }}"/>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {{ $data->appends(['name' => $name, 'shop_id' => $shopId, 'id' => $id, 'top_category_id' => $top_category_id, 'first_category_id' => $first_category_id, 'category_id' => $category_id, 'brand_id' => $brand_id])->links() }}
        </div>
    </div>
@endsection
@push('scripts')
    <script>
        $('.change-sort').on('change', function () {
            var sort = $(this).val();
            var id = $(this).attr('data-id');
            $.ajax({
                url: '{{ route('admin.good.sort') }}',
                type: "GET",
                dataType: 'json',
                data: {
                    id: id,
                    sort: sort,
                },
                success: function (re) {
                }
            });
        });

        // 选择顶级分类  查询对应一级分类和品牌
        $('#top_category_id').on('change', function () {

            top_category_id = $(this).val()
            shop_id = {{ $shopId }}
            // $('#top_category_id').empty()
            $('#first_category_id').empty()
            $('#category_id').empty()
            $('#brand_id').empty()

            // 一级分类
            $.ajax({
                url: '{{ route('admin.good.select_category') }}',
                type: "GET",
                dataType:'json',
                data: {
                    top_category_id: top_category_id,
                    first_category_id:0,
                    shop_id : shop_id
                },
                success: function (re) {
                    options = '';
                    for (var i = 0; i < re.length; i++) {
                        options += '<option value="' + re[i].id + '">' + re[i].name + '</option>'
                    }
                    $('#first_category_id').append(options)
                }
            });

            // 品牌
            $.ajax({
                url: '{{ route('admin.good.select_brand') }}',
                type: "GET",
                dataType:'json',
                data: {
                    pid: top_category_id,
                    shop_id : shop_id
                },
                success: function (re) {
                    options = '';
                    for (var i = 0; i < re.length; i++) {
                        options += '<option value="' + re[i].id + '">' + re[i].name + '</option>'
                    }
                    $('#brand_id').append(options)
                }
            });
        });

        // 选择一级分类 查询二级分类
        $('#first_category_id').on('change', function () {

            top_category_id = $('#top_category_id').find('option:selected').val()
            first_category_id = $(this).val()
            shop_id = {{ $shopId }}
            $.ajax({
                url: '{{ route('admin.good.select_category') }}',
                type: "GET",
                dataType:'json',
                data: {
                    top_category_id:top_category_id,
                    first_category_id:first_category_id,
                    shop_id : shop_id
                },
                success: function (re) {
                    $('#category_id').empty()
                    options = '';
                    for (var i = 0; i < re.length; i++) {
                        options += '<option value="' + re[i].id + '">' + re[i].name + '</option>'
                    }
                    $('#category_id').append(options)
                }
            });
        });

    </script>
@endpush
